using System.Collections.Generic;
using System.Linq;

namespace SportsStore.Domain.Enities
{
    public class Cart
    {
        private HashSet<CartLine> _lines;

        public Cart()
        {
            _lines=new HashSet<CartLine>();
        }
        public void AddItem(Product product, int quantity)
        {
            var line = _lines.FirstOrDefault(l=>l.Product.ProductID==product.ProductID);
            if (null==line)
            {
                var cartLine = new CartLine { Product=product,Quantity=quantity};
                _lines.Add(cartLine);
            }
            else
            {
                line.Quantity += quantity;
            }
            
        }

        public IEnumerable<CartLine> Lines
        {
            get { return _lines; }
        }

        public void Remove(Product product)
        {
            _lines.RemoveWhere(l => l.Product.ProductID == product.ProductID);
        }

        public decimal ComputeTotalValue()
        {
            return _lines.Sum(l => l.Product.Price * l.Quantity);
        }

        public void Clear()
        {
            _lines.Clear();
        }
    }
}